<?php

//	Author: 	Aris Michail
//	Email:		std03055@di.uoa.gr
//	Purpose: 	Get the values of the attributes of an entity

// REVISE attributes table on entity

require_once ("../DB/DB.php");
require_once ("../Frameworks/Objects/user.php");
require_once ("../Frameworks/Objects/category.php");

function DB_getEntities($category)
{

    global $conn;

    // The name of the stored procedure
    $sp_name = "usp_getEntities";

    // The return variable
    $retval;

    // Get the values
    $catid = $category->GetId();
    $entityname = '%';

    // Create a new statement
    $stmt = mssql_init($sp_name, $conn);

    // Bind values
    mssql_bind($stmt, "RETVAL", $retval, SQLINT4, true, false, 1);
    mssql_bind($stmt, '@categoryid', $catid, SQLINT4, false, false, 1);
    mssql_bind($stmt, '@entityname', $entityname, SQLVARCHAR, false, false, 50);

    // Execute the statement
    $status = mssql_execute($stmt);
    if ($status)
    {
        $i = '0';
        $myentities = array();
         /* the table that has all entities */
        $attributes = array();
         /* the table with the attributes of the category */
        $entitynames = array();
         /* the table with the names of all entities in category */
        $rvalue = DB_category_getattributes($category, $attributes);
         /* get the attributes of the category */
        if ($rvalue != 0)
            return $rvalue;

        for ($a = 0; $a < count($attributes); $a++)
            $attributes[$a]->setCategoryId($category->GetId());

        DB_category_getentitynames($category, $entitynames);
         /* get the names of the entities in category */

        for ($a = 0; $a < count($entitynames); $a++)
        {
            $myentities[$a] = new Entity();
             /* but not values */
            $myentities[$a]->SetName($entitynames[$a]);

            $myattrs = array();
            for ($p = 0; $p < count($attributes); $p++)
            {
                $myattrs[$p] = new Attribute();
                $myattrs[$p]->SetCategoryId($attributes[$p]->GetCategoryId());
                $myattrs[$p]->SetName($attributes[$p]->GetName());
                $myattrs[$p]->SetType($attributes[$p]->GetType());
                $myattrs[$p]->SetValue($attributes[$p]->GetValue());
            }

            $myentities[$a]->SetAttributes($myattrs);
            $myentities[$a]->SetCategoryId($category->getid());
            $id = DB_getEntityFromName($catid, $entitynames[$a]);
            $myentities[$a]->SetId($id);
        }

        $i = '0';
        while ($row = mssql_fetch_row($status))
        {
            for ($k = 0; $k < count($myentities); $k++)
            {
                if (strcmp($myentities[$k]->GetName(), $row[0]) == 0)
                {
                    $myentities[$k]->SetValueToAttribute($row[2], $row[1]);
                    $myentities[$k]->SetIdToAttribute($row[4], $row[1]);
                }
            }
            mssql_next_result($status);
        }
        return $myentities;
    }
}

?>